﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.IO;

namespace Barbar.Colonization.Core.Configuration {
  [XmlTypeAttribute(Namespace="http://codeplex.com/colonization/colonizationconfig.xsd")]
  public sealed class ColonizationConfig : IConfigurationSectionHandler {
    private string m_RuleSetPath;
    private string m_LanguagePath;

    private static ColonizationConfig ColonizationConfigInstance;
    private static object SyncRoot = new object();

    public static ColonizationConfig Instance {
      get {
        lock (SyncRoot) {
          if (ColonizationConfigInstance == null)
            ColonizationConfigInstance = (ColonizationConfig)ConfigurationManager.GetSection("ColonizationConfig");
          return ColonizationConfigInstance;
        }
      }
    }

    public string RuleSetPath {
      get { return m_RuleSetPath; }
      set {
        if (String.IsNullOrEmpty(value))
          throw new ArgumentNullException("value");
        m_RuleSetPath = Path.Combine(Utility.ApplicationPath, value);
      }
    }

    public string LanguagePath {
      get { return m_LanguagePath; }
      set {
        if (String.IsNullOrEmpty(value))
          throw new ArgumentNullException("value");
        m_LanguagePath = Path.Combine(Utility.ApplicationPath , value);
      }
    }

    #region IConfigurationSectionHandler Members

    object IConfigurationSectionHandler.Create(object parent, object configContext, XmlNode section) {
      ColonizationConfig colonizationConfig = null;
      using (XmlNodeReader reader = new XmlNodeReader(section)) {
        XmlSerializer serializer = new XmlSerializer(typeof(ColonizationConfig), "http://codeplex.com/colonization/colonizationconfig.xsd");
        colonizationConfig = (ColonizationConfig)serializer.Deserialize(reader);
        reader.Close();
      }
      return colonizationConfig;
    }
    #endregion
  }
}
